package com.zzwtec.web.controller;



import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zzwtec.ResultObject;
import com.zzwtec.SimpleResult;
import com.zzwtec.basedata.bean.Area;
import com.zzwtec.basedata.services.AreaService;
import com.zzwtec.web.exception.MyException;



/**
 * @author  草原狼
 *
 * 2017年7月5日
 */
@Controller
@RequestMapping("area")
public class AreaController {
	@Autowired
	private  HttpServletRequest request;
	@Autowired
	private AreaService areaService;
	 
	 /**
	  * 添加区域界面
	  * @return
	  */
	 @GetMapping("/add")
	 public String addAreaUI() {
		 return "/area/area_add";
	 }
	 
	/**
	 * 添加区域业务
	 * @return
	 */
	@RequestMapping(value="/add", method=RequestMethod.POST)	
	public String addArea(@ModelAttribute Area bean, ModelMap model) {
		model.put("msg", "添加区域失败");
		SimpleResult result = areaService.addArea(bean);
		if(result.getCode() == 0){
			model.put("msg", "添加区域成功");
		}
		return "redirect:/area/list";
	}
	
	/**
	 * 删除区域
	 * @param id
	 * @return
	 */
	@GetMapping("/delete/{id}")
	public String removeArea(@PathVariable("id") String id,ModelMap model) {
		model.put("msg", "删除区域失败");
		SimpleResult result = areaService.removeArea(id);
		if(result.getCode() == 0){
			model.put("msg", "删除区域成功");
		}
		return "redirect:/area/list";
	}
	
	/**
	 * 编辑区域
	 * @return
	 */
	@GetMapping("/edit/{id}")	
	public String modifyAreaUI(@PathVariable("id") String id,ModelMap model) {		
		ResultObject<Area> result = areaService.queryAreaById(id);
		if(result.getCode() == 0){
			model.put("area", result.getData());
		}else{
			model.put("msg", result.getMsg());
		}		
		return "/area/area_edit";
	}
	
	/**
	 * 提交编辑小区
	 * @param id
	 * @param bean
	 * @return
	 */
	@PostMapping("/upd")
	public String modifyArea(@ModelAttribute Area bean ,ModelMap model) {
		model.put("msg", "修改区域失败");
		SimpleResult result = areaService.modifyArea(bean);
		if(result.getCode() == 0){
			model.put("msg", "修改区域成功");
		}
		return "redirect:/area/list";
	}
	@GetMapping("/list")
	public String queryAllArea(ModelMap model) {
		ResultObject<List<Area>> result = areaService.queryAllArea();		
		model.put("list", result.getData());
		request.getSession().setAttribute("request Url", request.getRequestURL());  
		return "/area/area_list";
	}
	
	@GetMapping("/{id}")
	public String queryAreaById(@PathVariable("id") String id,ModelMap model) {
		ResultObject<Area> result = areaService.queryAreaById(id);		
		model.put("area", result.getData());
		return "/area/area_view";
	}
	
	@RequestMapping("/hello")
	@ResponseBody
    public ResultObject hello(HttpServletRequest request) throws Exception {
		request.getSession().setAttribute("request Url", request.getRequestURL());  
		return new ResultObject();
    }

    @RequestMapping("/json")
    public String json() throws MyException {
        throw new MyException("发生错误2");
    }
}
